package me.qi.kancha.controller;

import me.qi.kancha.controller.base.BaseController;
import me.qi.kancha.dto.base.BaseDTO;
import me.qi.kancha.dto.core.UserDTO;
import me.qi.kancha.exception.PreconditionException;
import me.qi.kancha.service.UserService;
import me.qi.kancha.service.base.support.PageData;
import me.qi.kancha.vo.user.UserBatchBindVo;
import me.qi.kancha.vo.user.UserBindVo;
import me.qi.kancha.vo.user.UserQueryVo;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户实体模块
 *
* @author sun
* @date 2022-08-21 12:36:21
* @modified By：
*/
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    private UserService service;

    public UserController(UserService service) {
        this.service = service;
    }

    /**
    * 用户实体模块-分页列表
    *
    * @param param
    * @return
    */
    @GetMapping
    public PageData<UserDTO> listPaging(@Validated UserQueryVo param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 listPaging 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.listPaging(param);
    }

    /**
    * 用户实体模块-新增
    *
    * @param param
    * @return
    */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public UserDTO insert(@Validated @RequestBody Object param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 insert 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.insert(param);
    }


    /**
     * 用户实体模块-批量启用/禁用
     *
     * @return
     */
    @PostMapping(value = "/enable/{enable:true|false}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity enable(@PathVariable Boolean enable, @RequestBody List<String> ids) {
        boolean update = service.lambdaUpdate()
                .set(UserDTO::getEnable, enable)
                .in(BaseDTO::getId, ids).update();
        return build(update, "批量更新失败");
    }

    /**
     * 用户实体模块-绑定角色
     *
     * @return
     */
    @PostMapping(value = "/bind", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity bind(@Validated @RequestBody UserBindVo param, BindingResult result) {
        checkBindingResult(result);
        UserDTO user = service.getById(param.getId());
        if (user == null) {
            throw new PreconditionException("绑定用户异常");
        }
        return build(service.bind(user, param.getCodes()), "批量更新失败");
    }

    /**
     * 用户实体模块-绑定角色
     *
     * @return
     */
    @PostMapping(value = "/batch/bind", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity bind(@Validated @RequestBody UserBatchBindVo param, BindingResult result) {
        return build(service.batchBind(param), "批量更新失败");
    }


    /**
    * 用户实体模块-修改
    *
    * @param param
    * @return
    */
    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public UserDTO update(@Validated @RequestBody Object param, BindingResult result) {
        //请根据需求重写定义入参, 并在 serviceImpl 冲重写 update 方法, 通过强转获得真正的入参类型
        checkBindingResult(result);
        return service.update(param);
    }

    /**
    * 用户实体模块-批量删除
    *
    * @param ids
    * @return
    */
    @PostMapping(value = "/deleteBatch", consumes = MediaType.APPLICATION_JSON_VALUE)
    public void deleteBatch(@RequestBody List<String> ids) {
        //如果有其他关联业务-在serviceImpl中重写 removeBatchByIds 方法
        service.removeBatchByIds(ids);
    }

    /**
    * 用户实体模块-删除
    *
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id:"+ ID_REGX +"}")
    public void delete(@PathVariable String id) {
        //如果有其他关联业务-在 serviceImpl 中重写 removeById 方法
        service.removeById(id);
    }

    /**
     * 用户实体模块-根据账号id查询详情
     *
     * @param aid
     * @return
     */
    @GetMapping(value = "/aid/{aid:"+ ID_REGX +"}")
    public UserDTO detailByAid(@PathVariable String aid) {
        //如果有其他关联业务-在 serviceImpl 中重写 removeById 方法
        return service.detailByAid(aid);
    }

    /**
     * 用户实体模块-用户权限信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/ruleCodes/{id:"+ ID_REGX +"}")
    public List<String> ruleCodes(@PathVariable String id) {
        //如果有其他关联业务-在 serviceImpl 中重写 removeById 方法
        return service.ruleCodes(id);
    }

    /**
    * 用户实体模块-详情
    *
    * @return
    */
    @GetMapping(value = "/{id:"+ ID_REGX +"}")
    public UserDTO detail(@PathVariable String id) {
        // 默认方法只根据id查询单表数据。如果接口需要返回关联表数据：
        // 1. 根据接口数据需求定义集成DTO的VO类
        // 2. serviceImpl重写getById 并返回VO 类
        // 3. 在controller 将结果强转成VO类返回给前端
        return service.getById(id);
    }

    /**
     * 用户实体模块-批量删除
     *
     * @return
     */
    @PostMapping(value = "/syncData")
    public boolean deleteBatch() {
        //如果有其他关联业务-在serviceImpl中重写 removeBatchByIds 方法
        return true;
    }

//    /**
//     * 根据用户名进行模糊查询用户信息
//     *
//     * @param name
//     * @return
//     */
//    @GetMapping("/infos")
//    public List<UserInfo> getUserInfosByName(@RequestParam("name") String name) {
//        return service.getUserInfosByName(name);
//    }


}




